package web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.Asset;
import domain.AssetAccount;
import domain.AssetChange;
import domain.Repair;
import domain.User;
import service.WorkerService;

@WebServlet("/worker.con")
public class WorkerController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		WorkerService workerService = new WorkerService(); 
		HttpSession session = request.getSession();
	
		if(request.getParameter("work").equals("inLoading")) {
			List<Asset> list = new ArrayList<>();
			list = workerService.queryInLoading(1);
			session.setAttribute("inLoading", list);
			request.getRequestDispatcher("worker/inLoading.jsp").forward(request, response);
			
		}else if(request.getParameter("work").equals("inNO")) {
			List<Asset> list = new ArrayList<>();
			list = workerService.queryInLoading(0);
			session.setAttribute("inLoading", list);
			request.getRequestDispatcher("worker/inNO.jsp").forward(request, response);
			
		}else if(request.getParameter("work").equals("addAsset")) {
			String name = request.getParameter("name");
			String category = request.getParameter("category");
			String number = request.getParameter("number");
			String belongTo = request.getParameter("belongTo");
			String address = request.getParameter("address");
			String purchaser = request.getParameter("purchaser");
			String purchasingValue = request.getParameter("purchasingValue");
			String storageTime = request.getParameter("storageTime");
			String state = workerService.addAsset(name,category,number,belongTo,address,purchaser,purchasingValue,storageTime);
			request.setAttribute("state", state);
			request.getRequestDispatcher("worker/worker.jsp?a=2").forward(request, response);
			
		}else if(request.getParameter("work").equals("addChange")) {
			String name = request.getParameter("name");
			String category = request.getParameter("category");
			String number1 = request.getParameter("number1");
			User user  = (User)session.getAttribute("user");
			String past = user.getBelongTo();
			String address = request.getParameter("address");
			String now = request.getParameter("now");
			String sellValue = request.getParameter("sellPrice");
			String header = request.getParameter("header");
			String auditor = request.getParameter("auditor");
			String sellTime = request.getParameter("sellTime");
			String state = workerService.addChange(name,category,number1,past,address,now,sellValue,header,auditor,sellTime);
			request.setAttribute("state", state);
			request.getRequestDispatcher("worker/worker.jsp?a=2").forward(request, response);
		
		}else if(request.getParameter("work").equals("changeLoading")) {
			List<AssetChange> list = new ArrayList<>();
			list = workerService.queryChangeLoading(1);
			session.setAttribute("changeLoading", list);
			request.getRequestDispatcher("worker/changeLoading.jsp").forward(request, response);
			
		}else if(request.getParameter("work").equals("changeOK")) {
			List<AssetChange> list = new ArrayList<>();
			list = workerService.queryChangeLoading(0);
			session.setAttribute("changeOK", list);
			request.getRequestDispatcher("worker/changOK.jsp").forward(request, response);
			
		}else if(request.getParameter("work").equals("queryMyAll")) {
			List<Asset> list = new ArrayList<>();
			User user  = (User)session.getAttribute("user");
			String past = user.getBelongTo();
			list = workerService.queryMyAll(past);
			session.setAttribute("queryMyAll", list);
			request.getRequestDispatcher("worker/queryMyAll.jsp").forward(request, response);
			
		}else if(request.getParameter("work").equals("queryAssetAccount")) {
			List<AssetAccount> list = new ArrayList<>();
			User user  = (User)session.getAttribute("user");
			String past = user.getBelongTo();
			list = workerService.queryOwnAssetAccount(past);
			session.setAttribute("queryAssetAccount", list);
			request.getRequestDispatcher("worker/queryMyAll.jsp").forward(request, response);

		}else if(request.getParameter("work").equals("addRepair")) {
			String id = request.getParameter("id");
			String number = request.getParameter("number");
			String repair = request.getParameter("repair");
			String cost = request.getParameter("cost");
			String belongTo = request.getParameter("past");
			String state = workerService.addRepair(belongTo,id,number,cost,repair);
			request.setAttribute("state", state);
			request.getRequestDispatcher("worker/worker.jsp?a=2").forward(request, response);
			
		}else if(request.getParameter("work").equals("queryRepair")) {
			List<Repair> list = new ArrayList<>();
			list = workerService.queryMyAllRepair();
			session.setAttribute("queryMyAllRepair", list);
			request.getRequestDispatcher("worker/queryMyAll.jsp").forward(request, response);
			
		}else if(request.getParameter("work").equals("endRepair")) {
			Repair repair = new Repair();
			repair.setId(Integer.parseInt(request.getParameter("id")));
			repair.setName(request.getParameter("name"));
			repair.setAddress(request.getParameter("address"));
			repair.setNumber2(Integer.parseInt(request.getParameter("number2")));
			repair.setRepair(request.getParameter("repair"));
			repair.setCost(Integer.parseInt(request.getParameter("cost")));
			User user  = (User)session.getAttribute("user");
			repair.setAuditor(user.getUserName());
			String state = workerService.endRepair(repair);
			request.setAttribute("state", state);
			request.getRequestDispatcher("worker/worker.jsp?a=2").forward(request, response);
			
		}
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
